﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace QuanLyNhaHang.DataAccess
{
    class DAOrder_LocationMst
    {
        DataAccess.SqlService service;
        public DAOrder_LocationMst()
        {
            service = new SqlService();
        }

        #region Location Managements
        public DataTable optionLoadLocation()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("STT");
            dt.Columns.Add("TEN_KHUVUC");

            SqlParameter[] param = { new SqlParameter("@TEN_KHUVUC", "") };
            DataTable dtTmp = service.getDTStoredProcedure("ad_KHUVUC_GetAll", param);

            int i = 1;
            foreach (DataRow row in dtTmp.Rows)
            {
                dt.Rows.Add(new object[] { i, row[0].ToString() });
                i++;
            }
            return dt;
        }

        public void optionNewLocation(String tenKV)
        {
            SqlParameter[] param = { new SqlParameter("@OPTION", "INSERT"), new SqlParameter("@TENKV", tenKV) };
            service.ExcuteStoredProcedure("ad_KHUVUC_Edit", param);
        }

        public bool optionCheckDupLocation(String tenKV)
        {
            String SQL = "Select COUNT(*) from KHU_VUC where TEN_KHUVUC = N'" + tenKV + "'";
            int i = (int)service.executeScalar(SQL);
            if (i != 0)
            {
                return false;
            }
            return true;
        }

        public void optionDelete(String tenKV)
        {
            SqlParameter[] param = { new SqlParameter("@TenKV", tenKV) };
            service.ExcuteStoredProcedure("ad_KHUVUC_DeleteS_WhereTENKV", param);
        }

        public void optionUpdateLocation(String strOld, String strNew)
        {   //update DS_BAN and KHU_VUC where TenKhuVuc - new - old.
            SqlParameter[] param = { new SqlParameter("@TenKVNew", strNew), new SqlParameter("@TenKVOld", strOld) };
            service.ExcuteStoredProcedure("ad_KHUVUC_UpdateS_WhereTENKV", param);
        }

        public void optionAddFirstTable(String tenKV)
        {
            int j = -1;
            String temp = null;

            do
            {
                // Random 1 number, and check number on DS_Ban. IF duplicate => ramdom else use for name Table
                Random random = new Random();
                int i = random.Next(0, 1000);
                temp = "Bàn " + i;

                String sqlTemp = "select COUNT(*) from DS_BAN where TENBAN = '" + tenKV + "'";
                j = (int)service.executeScalar(sqlTemp);
            } while (j != 0);

            //Insert values to DSBan
            SqlParameter[] param = { new SqlParameter("@OPTION", "INSERT"), new SqlParameter("@TENBAN", temp), new SqlParameter("@TENKV", tenKV) };
            service.executeScalarSProcedure("ad_DSBAN_Edit", param);
        }
        #endregion
    }
}
